/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package telsoft.cms_admin.login;

import com.ipcc.authentication.TNAuthentication;
import com.ipcc.utility.entity.Menu;
import java.io.Serializable;
import java.sql.Connection;
import java.util.ArrayList;
import java.util.Vector;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import telsoft.admin.util.AppAuthenticator;
import telsoft.admin.util.AppServer;
import telsoft.cms_admin.validation.redirect;
import telsoft.sql.Database;
import telsoft.util.StringUtil;
/**
 *
 * @author tuanta
 */
@ManagedBean
@SessionScoped
public class LoginBean implements Serializable {
    private String strUserName;
    private String strPassword = "";
    private String strMsgError = "";
    private Vector vtMenu, vtRule, vtRule_Detail, vtRuleResuft;
    private ArrayList<Menu> menu = new ArrayList<Menu>();
    public static final String AUTH_KEY = "au_key_usr";
    public static final String USER_ID = "au_user_id";
    public static final String AUTH_MENU_KEY = "au_key_menu";
    public static final String IS_ADMIN = "is_admin";
    String userID;
    private boolean mbRenderedError = false;
    private int sizeLager;
    public Object oldURL;

    public String doLogin() {
        Connection cn = null;
        try {
            cn = AppServer.getConnection();
            AppAuthenticator aa = new AppAuthenticator();
            aa.setConnection(cn);
            userID = aa.verifyPassword(strUserName, StringUtil.encrypt(strPassword, "SHA"));
            mbRenderedError = false;
            TNAuthentication tna = new TNAuthentication();
            tna.setCn(cn);
            boolean is_admin = tna.isAdmin(Integer.parseInt(userID));
            
            
            FacesContext.getCurrentInstance().getExternalContext().getSessionMap().put(AUTH_KEY, strUserName);
            FacesContext.getCurrentInstance().getExternalContext().getSessionMap().put(USER_ID, userID);
            FacesContext.getCurrentInstance().getExternalContext().getSessionMap().put(IS_ADMIN, is_admin);
            
            
            menu = RuleBean.createMenu(String.valueOf(userID));
            
            FacesContext fc = FacesContext.getCurrentInstance();
            HttpServletRequest req = (HttpServletRequest) fc.getExternalContext().getRequest();
            HttpSession session = req.getSession(false);
            oldURL = session ==null?null: session.getAttribute("oldURL");
            if (oldURL != null) {
                return oldURL + "?faces-redirect=true";
            }
            
        } catch (Exception e) {
            mbRenderedError = true;
            e.printStackTrace();            
            if(e.getMessage().equals("TS-00006")){
                strMsgError = "Thông tin tài khoản không chính xác!" + e.getMessage();
            }
            else strMsgError = "Kết nối đến cơ sở dữ liệu có sự cố xảy ra!" + e.getMessage();
            return "/tamnhat/login?faces-redirect=true";
        }
        finally{
            Database.closeObject(cn);
        }
        return "/tamnhat/home?faces-redirect=true";
    }

    public String doLogout() {
        oldURL = "";
        FacesContext.getCurrentInstance().getExternalContext().getSessionMap().remove(AUTH_KEY);
        FacesContext.getCurrentInstance().getExternalContext().getSessionMap().remove(USER_ID);
        FacesContext.getCurrentInstance().getExternalContext().getSessionMap().remove("menuBean");
        FacesContext.getCurrentInstance().getExternalContext().getSessionMap().clear();
        redirect rd = new redirect();
        boolean setinfo = false;
        rd.setInfo(setinfo);
        return "/tamnhat/login.xhtml?faces-redirect=true";
    }

    public boolean isMbRenderedError() {
        return mbRenderedError;
    }
    public int getSizeLager() {
        return sizeLager;
    }
    public String getStrMsgError() {
        return strMsgError;
    }

    public String getStrPassword() {
        return strPassword;
    }

    public String getStrUserName() {
        return strUserName;
    }

    public Vector getVtMenu() {
        return vtMenu;
    }

    public Vector getVtRule() {
        return vtRule;
    }

    public Vector getVtRuleResuft() {
        return vtRuleResuft;
    }

    public Vector getVtRule_Detail() {
        return vtRule_Detail;
    }

    public void setMbRenderedError(boolean mbRenderedError) {
        this.mbRenderedError = mbRenderedError;
    }

    public void setSizeLager(int sizeLager) {
        this.sizeLager = sizeLager;
    }

    public void setStrMsgError(String strMsgError) {
        this.strMsgError = strMsgError;
    }

    public void setStrPassword(String strPassword) {
        this.strPassword = strPassword;
    }

    public void setVtMenu(Vector vtMenu) {
        this.vtMenu = vtMenu;
    }

    public void setVtRule(Vector vtRule) {
        this.vtRule = vtRule;
    }

    public void setVtRuleResuft(Vector vtRuleResuft) {
        this.vtRuleResuft = vtRuleResuft;
    }

    public void setVtRule_Detail(Vector vtRule_Detail) {
        this.vtRule_Detail = vtRule_Detail;
    }

    public void setStrUserName(String strUserName) {
        this.strUserName = strUserName;
    }

    public ArrayList<Menu> getMenu() {
        return menu;
    }

    public void setMenu(ArrayList<Menu> menu) {
        this.menu = menu;
    }

    public String getOldURL() {
        return oldURL.toString();
    }

    public void setOldURL(String oldURL) {
        this.oldURL = oldURL;
    }
}
